package com.sf.web.controller;

import com.sf.domain.Employee;
import com.sf.service.IEmployeeService;
import com.sf.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * restController = controller+ resposeBody
 * getMapping  postMapping  deleteMapping  putMapping  @PathVarianle
 */
@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有用户信息
     * 请求路径: /employees
     * 请求方式: GET
     * 请求参数: 无
     * 请求返回值: {success: true,msg: 操作成功,data:List<Employee>}
     */

//    @RequestMapping(value = "/employees",method = RequestMethod.GET)
    @GetMapping
    public JsonResult list(){
        return new JsonResult(true,"操作成功",employeeService.list());
    }


    /**
     * 新增用户信息
     * 请求路径: /employees
     * 请求方式: POST
     * 请求参数: 无
     * 请求返回值: {success: true,msg: 操作成功,data:null}
     */
    @PostMapping
    public JsonResult save(Employee employee){
        employeeService.insert(employee);
        return new JsonResult(true,"操作成功",null);
    }


    /**
     * 更新用户信息
     * 请求路径: /employees
     * 请求方式: PUT
     * 请求参数: 无
     * 请求返回值: {success: true,msg: 操作成功,data:null}
     */
    @PutMapping
    public JsonResult update(Employee employee){
        employeeService.update(employee);
        return new JsonResult(true,"操作成功",null);
    }

    /**
     * 更新用户信息
     * 请求路径: /employees
     * 请求方式: DELETE
     * 请求参数: id
     * 请求返回值: {success: true,msg: 操作成功,data:null}
     */
    @DeleteMapping
    public JsonResult delete(Long id){
        employeeService.delete(id);
        return new JsonResult(true,"操作成功",null);
    }

    /**
     * 更新用户信息
     * 请求路径: /employees
     * 请求方式: Get
     * 请求参数: id
     * 请求返回值: {success: true,msg: 操作成功,data:Employee}
     */
//    @RequestMapping(value = "/employees/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable Long id){
        return new JsonResult(true,"操作成功",employeeService.get(id));
    }
}
